# coding=utf-8
##
## Author: jmdvirus@aliyun.com
##
## Create: 2019年02月15日 星期五 13时42分58秒
##

import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics

def load_data():
    x = np.linspace(0, 10, 100)
    print("x : ", x)
    y_true = np.sin(x) + np.random.rand(x.size) - 0.5
    y_pred = np.sin(x)

    print("y_true: ", y_true)
    print("y_pred: ", y_pred)
    return (x, y_true, y_pred)

def show(orig, y_true, y_pred):
    plt.style.use('ggplot')
    plt.plot(orig, y_pred, linewidth=4, label='model')
    plt.plot(orig, y_true, 'o', label='data')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend(loc='lower left')
    plt.show()

def cal(y_true, y_pred):
    mse = np.mean((y_true - y_pred) ** 2)
    print("mse: ", mse)
    mse2 = metrics.mean_squared_error(y_true, y_pred)
    print("mse2: ", mse2)
    fvu = np.var(y_true - y_pred)/np.var(y_true)
    print("fvu: ", fvu)
    fve = 1.0 - fvu

    var = metrics.explained_variance_score(y_true, y_pred)
    print("var : ", var)

    r2 = 1.0 - mse/np.var(y_true)
    print("r2: ", r2)
    r22 = metrics.r2_score(y_true, y_pred)
    print("r22: ", r22)

    rr2 = metrics.r2_score(y_true, np.mean(y_true) *np.ones_like(y_true))
    print("rr2: ", rr2)

if __name__ == "__main__":
    t, x, y = load_data()
    #show(t, x, y)
    cal(x, y)

